package com.gxuwz.weather.api;

import com.baidubce.http.ApiExplorerClient;
import com.baidubce.http.AppSigner;
import com.baidubce.http.HttpMethodName;
import com.baidubce.model.ApiExplorerRequest;
import com.baidubce.model.ApiExplorerResponse;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonArray;

public class RealTimeRequest {
    public interface WeatherCallback {
        void onSuccess(String temperature, String weather, String windDirection, String windSpeed, String aqi,
                       String sunrise, String sunset, JsonArray hourlyData, JsonObject now, JsonObject day, JsonObject night, JsonObject data,JsonObject lifeIndex);
        void onError(String error);
    }

    public static void getWeather(String city, WeatherCallback callback) {
        new Thread(() -> {
            try {
                String path = "http://getweather.api.bdymkt.com/lundear/weather1d";
                ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.GET, path);
                request.setCredentials("bd0695e091ee4f83a9e4739a5a6dd3d5", "c3e30f7501024ba1aa958007d6377769");

                request.addHeaderParameter("Content-Type", "application/json;charset=UTF-8");
                request.addQueryParameter("areaCn", city);
                request.addQueryParameter("areaCode", "");
                request.addQueryParameter("ip", "");
                request.addQueryParameter("lat", "");
                request.addQueryParameter("lng", "");
                request.addQueryParameter("need3hour", "1");
                request.addQueryParameter("needIndex", "1");
                request.addQueryParameter("needObserve", "");
                request.addQueryParameter("needalarm", "1");
                request.addQueryParameter("need1hour", "1");

                ApiExplorerClient client = new ApiExplorerClient(new AppSigner());
                ApiExplorerResponse response = client.sendRequest(request);
                String result = response.getResult();
                System.out.println("接收="+response.getResult());
                
                JsonObject jsonObject = JsonParser.parseString(result).getAsJsonObject();
                String code = jsonObject.get("code").getAsString();

                if ("0".equals(code)) {
                    JsonObject data = jsonObject.getAsJsonObject("data");
                    if (data != null) {
                        JsonObject now = data.getAsJsonObject("now");
                        JsonObject day = data.getAsJsonObject("day");
                        JsonObject night = data.getAsJsonObject("night");
                        JsonArray hour3data = data.getAsJsonArray("hour3data");
                        JsonObject lifeIndex = data.getAsJsonObject("lifeIndex");
                        
                        if (now != null && day != null && night != null) {
                            String temp = now.get("temp").getAsString();
                            String weather = now.get("weather").getAsString();
                            String windDirection = now.get("WD").getAsString();
                            String windSpeed = now.get("WS").getAsString();
                            String aqi = now.get("aqi").getAsString();
                            
                            // 获取日出日落时间
                            String sunrise = day.get("sunUp").getAsString().replace("日出 ", "");
                            String sunset = night.get("sunDown").getAsString().replace("日落 ", "");
                            
                            callback.onSuccess(temp, weather, windDirection, windSpeed, aqi, sunrise, sunset, hour3data, now,day,night,data,lifeIndex);
                            return;
                        }
                    }
                }
                callback.onError("获取天气数据失败");
            } catch (Exception e) {
                callback.onError("发生错误：" + e.getMessage());
            }
        }).start();
    }
}
